<!--
 * @Author: 刘谕函1 11609278+liu-yuhan-1@user.noreply.gitee.com
 * @Date: 2023-01-11 11:37:02
 * @LastEditors: 刘谕函1 11609278+liu-yuhan-1@user.noreply.gitee.com
 * @LastEditTime: 2023-01-11 11:55:31
 * @FilePath: \寒假作业\vue-admin-template-master\src\views\employees\components\assign_role.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<!--  -->
<template>
  <el-dialog title="分配角色" :visible="showRoleDialog">
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <!-- 分配角色 -->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small">确定</el-button>
        <el-button size="small">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleList } from "@/api/setting";
import { getUserDetailById } from "@/api/user";
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false,
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null,
    },
  },
  data() {
    return {
      list: [], // 角色列表
      roleIds: [],
    };
  },
  created() {
    this.getRoleList();
  },

  components: {},

  computed: {},

  mounted() {},

  methods: {
    //  获取所有角色
    async getRoleList() {
      const { rows } = await getRoleList();
      this.list = rows;
    },

    async getUserDetailById(id) {
      const { roleIds } = await getUserDetailById(id);
      this.roleIds = roleIds; // 赋值本用户的角色
    },

    async btnOK() {
      await assignRoles({ id: this.userId, roleIds: this.roleIds });
      //   关闭窗体
      this.$emit("update:showRoleDialog", false);
    },
    btnCancel() {
      // 重置数据
      this.roleIds = []
      this.$emit('update:showRoleDialog', false)
    }
    
  },
  watch: {},
  mixins: [],
};
</script>
<style  scoped>
</style>